package model

import business.*
import enums.Direction
import org.itheima.kotlin.game.core.Painter
import kotlin.random.Random

class Enemy(i:Int,j:Int) :Moveable,AutoMove,Blockable,Sufferable,Destroyable {
    override fun isDestroy(): Boolean {
        if(blood<=0){
            return true
        }
        return false
    }

    override var blood=3

    override fun notifySuffer(attack: Attackable): Array<IView>? {
        if(attack.owner==this||attack.owner is Enemy){
            return null
        }
        blood-=attack.attackPower
        return arrayOf(Blast(x, y))
    }

    override var badDirection: Direction? = null
    override var width: Int=Config.block
    override var height: Int=Config.block
    override var x: Int=j*width
    override var y: Int=i*height
    override val speed=Config.block/8
    override var direction=Direction.DOWN
    var rand=1
    init {
        rand=Random.nextInt(1,7)
    }

    override fun draw() {
        val img_path=when(direction){
            Direction.UP -> "img/enemy_${rand}_u.gif"
            Direction.DOWN -> "img/enemy_${rand}_d.gif"
            Direction.LEFT -> "img/enemy_${rand}_l.gif"
            Direction.RIGHT -> "img/enemy_${rand}_r.gif"
            else -> throw Exception("坦克方向异常")
        }
        Painter.drawImage(img_path,x,y)
    }

    var lastMoveTime=0L
    var currMoveTime=0L
    var moveFrequency=100

    override fun autoMove(){
        currMoveTime=System.currentTimeMillis()
        if(currMoveTime-lastMoveTime<=moveFrequency){
            return
        }
        lastMoveTime=currMoveTime
        //println("direction:$direction,badDirection:$badDirection")
        if(y<0){
            y=0
            this.badDirection=Direction.UP
        }
        if(x<0){
            x=0
            this.badDirection=Direction.LEFT
        }
        if(x>Config.gameWidth-this.width){
            x=Config.gameWidth-this.width
            this.badDirection=Direction.RIGHT
        }
        if(y>Config.gameHeight-this.height){
            y=Config.gameHeight-this.height
            this.badDirection=Direction.DOWN
        }

        if(direction==badDirection){
            randDirection()
            return
        }
        when(direction){
            Direction.UP -> y-=speed
            Direction.DOWN -> y+=speed
            Direction.LEFT -> x-=speed
            Direction.RIGHT -> x+=speed
        }


    }

    /**
     * 随机改变方向
     */
    fun randDirection() {
        val rand = Random.nextInt(4)
        var dir = when (rand) {
            0 -> Direction.UP
            1 -> Direction.DOWN
            2 -> Direction.LEFT
            3 -> Direction.RIGHT
            else -> Direction.UP
        }
        if (dir == badDirection) {
            randDirection()
        } else {
            direction = dir
        }
    }

    var lastFireTime=0L
    var currFireTime=0L
    var fireFrequency=800L
    var bullet:Bullet?=null
    /**
     * 发射子弹
     */
    fun fire(){
        currFireTime=System.currentTimeMillis()
        //println(currFireTime-lastFireTime)
        if(currFireTime-lastFireTime<=fireFrequency){
            bullet=null
            return
        }
        lastFireTime=currFireTime
        bullet=Bullet(this,direction, {
                w,h->
            var xx:Int=0
            var yy:Int=0
            when(direction){
                Direction.UP -> {
                    xx=this.x+this.width/2-w/2
                    yy=this.y-h/2
                }
                Direction.DOWN -> {
                    xx=this.x+this.width/2-w/2
                    yy=this.y+this.height-h/2
                }
                Direction.LEFT -> {
                    xx=this.x-w/2
                    yy=this.y+this.height/2-h/2
                }
                Direction.RIGHT -> {
                    xx=this.x+this.width-w/2
                    yy=this.y+this.height/2-h/2
                }
            }
            Pair<Int,Int>(xx,yy)
        })
    }
}